package com.amigo.online.consumer.user.controller;


import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.amigo.online.common.core.response.Meta;
import com.amigo.online.common.core.response.StatusCode;
import com.amigo.online.common.util.UploadTempUtil;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.amigo.online.common.base.controller.BaseConsumerController;
import com.amigo.online.common.core.response.Response;
import com.amigo.online.common.param.dto.group.AddGroupMemberDto;
import com.amigo.online.common.param.dto.group.EditGroupDto;
import com.amigo.online.common.param.dto.group.NewGroupDto;
import com.amigo.online.common.param.dto.group.QuitGroupDto;
import com.amigo.online.common.param.dto.group.UpdateGroupDto;
import com.amigo.online.consumer.user.service.GroupService;
import com.amigo.online.consumer.user.util.UploadImgUtil;

/**
 * 群聊
 * @author kml
 *
 */
@RestController
@RequestMapping(value = "/group", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class GroupController extends BaseConsumerController {

	@Autowired
	GroupService groupService;
	
	
	//新建一个群组
	@PostMapping(value = "/newGroup")
	public ResponseEntity<Response> newGroup(HttpServletRequest request) {
		NewGroupDto dto = (NewGroupDto) getDto(request,NewGroupDto.class);
		return groupService.newGroup(dto);
	}
	
	//退出一个群组
	@PutMapping(value = "/quitGroup")
	public ResponseEntity<Response> quitGroup(HttpServletRequest request) {
		QuitGroupDto dto = (QuitGroupDto) getDto(request,QuitGroupDto.class);
		return groupService.quitGroup(dto);
	}
	
	//群设置
	@PutMapping(value = "/editGroup")
	public ResponseEntity<Response> editGroup(HttpServletRequest request) {
		EditGroupDto dto = (EditGroupDto) getDto(request,EditGroupDto.class);
		return groupService.editGroup(dto);
	}
	
	
	//获得可以拉进群的好友
	@GetMapping(value = "/getPullableFriends/{groupId}")
	public ResponseEntity<Response> getPullableFriends(@PathVariable("groupId") Long groupId) {
		return groupService.getPullableFriends(groupId);
	}
	
	
	//将用户添加进群
	@PostMapping(value = "/addGroupMember")
	public ResponseEntity<Response> addGroupMember(HttpServletRequest request) {
		AddGroupMemberDto dto = (AddGroupMemberDto) getDto(request,AddGroupMemberDto.class);
		return groupService.addGroupMember(dto);
	}
	
	//获得我的入群信息和建群信息
	@GetMapping(value = "/getMyGroup")
	public ResponseEntity<Response> getMyGroup() {
		return groupService.getMyGroup();
	}
	
	
	/**
	 * 根据群ID查群成员信息
	 * @return
	 */
	@GetMapping(value = "/getMembers/{groupId}/{page}/{size}")
	public ResponseEntity<Response> getMembersByGroupId(@PathVariable Long groupId,@PathVariable(value = "page") Integer page,@PathVariable(value = "size") Integer size) {
		return groupService.getMembersByGroupId(groupId,page,size);
	}

	@GetMapping(value = "/nearby/{page}")
	public ResponseEntity<Response> nearbyGroup(@PathVariable Integer page,
										 @RequestHeader("lng") String lng,
										 @RequestHeader("lat") String lat) {
		return groupService.nearbyGroup(page, lng, lat);
	}

	/**
	 * 根据群ID查群成员数量
	 * @return
	 */
	@GetMapping(value = "/getMembersCount/{groupId}")
	public ResponseEntity<Response> getMembersCountByGroupId(@PathVariable("groupId") Long groupId) {
		return groupService.getMembersCountByGroupId(groupId);
	}
	
	/**
	 * 修改群头像
	 * @return
	 */
	@PostMapping(value = "/updateAvatar")
	public ResponseEntity<Response> updateAvatar(@RequestPart(value="images") MultipartFile[] images,
												 @RequestHeader(value = "ACCESS_TOKEN") String token,
												 UpdateGroupDto dto) throws IOException {
		String result = UploadTempUtil.uploadResourceImg(images[0], token);
		Response parseObject = new Gson().fromJson(result, Response.class);
		if(((Meta)parseObject.getMeta()).getCode() == 401) {
			return failure(StatusCode.UNAUTHORIZED);
		}
		dto.setAvatar(getAddress(parseObject));
		// TODO: 2019/10/11 重构时需要注意
		return groupService.updateGroup(dto);
	}
	private String getAddress(Response parseObject) {
		String address = "";
		if (parseObject != null && parseObject.getData() != null) {
			Object data = parseObject.getData();
			if (data instanceof List) {
				List<String> addressList = (List<String>) data;
				address = addressList.get(0);
			}
		}
		return address;
	}
	
	/**
	 * 
	 * @Title: updateAvatar   
	 * @Description: (修改群头像)
	 * @param: @param dto
	 * @param: @return      
	 * @return: ResponseEntity<Response> 
	 * @author: zxy  QQ:1363320658
	 * @date:   2019年1月14日 下午2:17:11
	 * @version V1.0      
	 * @throws
	 */
	@PostMapping(value = "/updateAvatar/v1")
	public ResponseEntity<Response> updateAvatar(UpdateGroupDto dto) {
		return groupService.updateGroup(dto);
	}
	
	
}
